﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb.Task
{
    public partial class EditPreContractComments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Header.Controls.Add(new LiteralControl(@"<base target=""_self"" />"));

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            if (!Page.IsPostBack)
            {
                //this.btnSave.Visible = false;
                
                //if (Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString()) || Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Editer.ToString()))
                //{
                //    this.btnSave.Visible = true;
                //}
                this.btnSave.Attributes.Add("onClick", "ConfirmSave('SureButton');"); 
                if (!Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString()))
                {
                    this.rdoRisk.Enabled = false;
                    this.rdoSafe.Enabled = false;
                }

                InitDate();

            }
            
        }

        private string PreContractID
        {
            get
            {
                if (Session["PreContractID"] == null) return "0";

                return (string)Session["PreContractID"];
            }
            set { Session["PreContractID"] = value; }

        }

        private void InitDate()
        {
            string id = Page.Request.QueryString["id"];

            if (id == null)
                return;

            DateTime date;
            if (id == "0")
            {
                //Joan 2008-10-20 format Date
                date = System.DateTime.Now;
                this.txtCreatedDate.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
                this.txtCreatedDate.ReadOnly = true;

                this.txtModified.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
                //end
                this.txtModifiedBy.Text = ((BEGDC.PreContract.BLL.Authentication.PCIdentity)Page.User.Identity).Name;
                this.txtCreator.Text = this.txtModifiedBy.Text;

                BEGDC.PreContract.BLL.IPreContractInfo precontractService = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];
                BEGDC.PreContract.DAL.PreContractInfo precontract = precontractService.GetPreContractInfo(Convert.ToInt32(this.PreContractID));

                this.txtPreContractNo.Text = precontract.PreContractNo;
                this.txtBD.Text = precontract.BDID.BDCode;
                return;
            }
            BEGDC.PreContract.BLL.IPreContractInfoComment commentService = (BEGDC.PreContract.BLL.IPreContractInfoComment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfoComment_Service];
            BEGDC.PreContract.DAL.PreContractInfoComment comments = commentService.GetPreContractInfoComment(int.Parse(id));

            if (comments == null)
            {
                return;
            }

            this.txtPreContractNo.Text = comments.PreContractID.PreContractNo;
            this.txtBD.Text = comments.PreContractID.BDID.BDCode;
            this.txtBD.ReadOnly = true;
            this.txtPreContractNo.ReadOnly = true;

            //Joan 2008-10-20 format Date
            if (comments.DateCreated != Nullables.NullableDateTime.Default)
            {
                date = System.Convert.ToDateTime(comments.DateCreated.Value);
                this.txtCreatedDate.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
            }

            this.txtCreator.Text = comments.Creator.CName;
            this.txtCreator.ReadOnly = true;
            this.txtModifiedBy.Text = comments.ModifiedBy.CName;
            if (comments.DateModified != Nullables.NullableDateTime.Default)
            {
                date = System.Convert.ToDateTime(comments.DateModified.Value);
                this.txtModified.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
            }
            //end

            if (comments.CommentPromisedDate.HasValue)
                this.txtPromisedDate.Text = comments.CommentPromisedDate.Value.ToString("yyyy-MM-dd");

            if (comments.IsRisk)
            {
                this.rdoRisk.Checked = true;
                this.rdoSafe.Checked = false;
            }
            else
            {
                this.rdoRisk.Checked = false;
                this.rdoSafe.Checked = true;
            }

            this.txtComments.Text = comments.Comment;

        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            string id = Page.Request.QueryString["id"];

            if (id == null)
                return;

            try
            {
                BEGDC.PreContract.BLL.IPreContractInfoComment commentsService = (BEGDC.PreContract.BLL.IPreContractInfoComment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfoComment_Service];
                BEGDC.PreContract.DAL.PreContractInfoComment comments = new BEGDC.PreContract.DAL.PreContractInfoComment();
                if (id != "0")
                {
                    comments = commentsService.GetPreContractInfoComment(int.Parse(id));
                }

                if (comments == null)
                    return;

                comments.Comment = this.txtComments.Text;

                BEGDC.PreContract.DAL.Employee employee = new BEGDC.PreContract.DAL.Employee();
                employee.EmployeeID = ((BEGDC.PreContract.BLL.Authentication.PCIdentity)Page.User.Identity).EmployeeId;

                comments.ModifiedBy = employee;
                comments.DateModified = System.DateTime.Now;

                if (this.txtPromisedDate.Text != string.Empty)
                    comments.CommentPromisedDate = Convert.ToDateTime(this.txtPromisedDate.Text);
                else
                    comments.CommentPromisedDate = null;

                if (this.rdoSafe.Checked)
                    comments.IsRisk = false;
                else
                    comments.IsRisk = true;
                BEGDC.PreContract.BLL.IPreContractInfo precontractService = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];
                BEGDC.PreContract.DAL.PreContractInfo precontract = new BEGDC.PreContract.DAL.PreContractInfo();
                bool bRiskUpd = false;
                if (id == "0")
                {
                    precontract.PreContractID = Convert.ToInt32(this.PreContractID);
                    comments.PreContractID = precontract;

                    comments.Creator = employee;
                    comments.DateCreated = System.DateTime.Now; 

                    comments.Create();

                    //Joan 2008-10-20 Update pre-contract secure/risk status
                    precontract.IsRisk = comments.IsRisk;
                    precontractService.UpdatePreContractRiskStat(precontract);
                    bRiskUpd = true;
                }
                else
                {
                    comments.Update();
                    //Joan 2008-10-20 if last comment, then Update pre-contract secure/risk status
                    DataTable dt=commentsService.GetLatestCommentID(comments.PreContractID.PreContractID);
                    if (dt.Rows[0][0].ToString() == comments.PreContractInfoCommentID.ToString())
                    {
                        precontract.PreContractID = comments.PreContractID.PreContractID;
                        precontract.IsRisk = comments.IsRisk;
                        precontractService.UpdatePreContractRiskStat(precontract);
                        bRiskUpd = true;
                    }
                }

                //Joan 2008-10-20 add arg for update pre-contract risk status
                string script = "var arr=new Array(); arr[0]='" + "Save" + "';arr[1]='" + bRiskUpd.ToString() + "'; arr[2]='" + precontract.IsRisk.ToString() + "';window.returnValue=arr;";
                Page.Response.Write(BaseFunction.GetAlertByReturnValue(true, script));

            }
            catch (Exception ex)
            {

                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {

                LinkButton lnkARID = (LinkButton)e.Row.FindControl("lnkDateCreated");
                lnkARID.Attributes.Add("onclick", "javascript:ShowPage('" + e.Row.Cells[0].Text.Trim() + "');");
                lnkARID.Text = e.Row.Cells[1].Text.Trim();

            }

        }
    }
}
